package com.leo;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.OverrunStyle;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.stage.PopupWindow;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

/**
 * @ClassName TooltipDemo
 * @Description
 * @Author wangss
 * @date 2020.06.01 23:32
 * @Version 1.0
 */
public class TooltipDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(an);

        Tooltip tooltip = new Tooltip("这是提示的内容");
        tooltip.setFont(new Font(20d));
        tooltip.setPrefWidth(200);
        tooltip.setPrefHeight(200);
        tooltip.setWrapText(true);
        // 省略号
        tooltip.setTextOverrun(OverrunStyle.CENTER_WORD_ELLIPSIS);
        tooltip.setTextAlignment(TextAlignment.CENTER);

        // 提示框显示位置
//        tooltip.setAnchorLocation(PopupWindow.AnchorLocation.CONTENT_BOTTOM_LEFT);

        // 透明度
//        tooltip.setOpacity(0.5);

        // 设置背景颜色

        // 自动隐藏
//        tooltip.setAutoHide(true);

        Button b1 = new Button("button1");
        b1.setTooltip(tooltip);

        AnchorPane.setTopAnchor(b1, 100d);
        AnchorPane.setLeftAnchor(b1, 100d);
        an.getChildren().add(b1);
        primaryStage.setScene(scene);
        primaryStage.setTitle("FileChooser");
        primaryStage.setWidth(600d);
        primaryStage.setHeight(600d);
        primaryStage.setResizable(false);
        primaryStage.show();

        tooltip.setOnShown(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                System.out.println("显示");
            }
        });

        tooltip.setOnHidden(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                System.out.println("隐藏");
            }
        });
    }
}
